import 'package:flutter/material.dart';
import 'package:lib_core/lib_core.dart';
import 'package:lib_ui/lib_ui.dart';

class DidChangeDependenciesPage extends StatefulWidget {
  const DidChangeDependenciesPage({super.key});

  @override
  State<DidChangeDependenciesPage> createState() => _DidChangeDependenciesPageState();
}

class _DidChangeDependenciesPageState extends State<DidChangeDependenciesPage> {
  bool flag = false;

  @override
  Widget build(BuildContext context) {
    const widget = MyStatefulWidget();
    return Scaffold(
      appBar: TransparentAppBar(title: 'DidChangeDependencies触发'),
      body: SizedBox(
        width: Get.width,
        child: Column(
          children: [
            flag ? Container(color: Colors.red, child: widget) : const Row(children: [widget]),
            Switch(
              value: flag,
              onChanged: (value) {
                setState(() {
                  flag = !flag;
                });
              },
            ),
            Text('这种就是子组件的整个父组件发生变化了，也就是常说的子组件的依赖变化了，这就会触发依赖变化回调，依赖变化是不会触发didUpdateWidget')
          ],
        ),
      ),
    );
  }
}

class MyStatefulWidget extends StatefulWidget {
  const MyStatefulWidget({super.key});

  @override
  State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  String text = '';

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    text = 'init';
  }

  @override
  Widget build(BuildContext context) {
    return Text('哈哈', style: LDFont.regular());
  }

  @override
  void didUpdateWidget(covariant MyStatefulWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    // 这个是不会触发的（除非设置了GlobalKey强制复用）
    'didUpdateWidget'.showSuccess();
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    '依赖发生变化'.showSuccess();
  }
}
